package innatour;

import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

public class HotelDetails extends innatour.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
  
  
  @Test
  public void Roomsearch() throws Exception {
	 driver.get("https://inna.ru/#/packages/details/6733-6623-01.08.2015-02.08.2015-0-1--348114-1689078727-1689079387-4?action=buy&displayHotel=348114#SectionRoom");
	
	  for (int second = 0;; second++) {
	    	if (second >= 180) fail("timeout");
	    	try { if (isElementPresent(By.xpath("//*[@class='hotel-details-rooms hotel-details-room_show_NONE']"))) break; } catch (Exception e) {}
	    	Thread.sleep(1000);
	    }
	 int numberscount = driver.findElements(By.xpath("//span[contains(.,'Купить')]")).size();
	  
	 WebElement button=null;
	 System.out.println(numberscount);
	  
	  for(int i=1;i<numberscount;i++)
	  {
		  WebElement hotelBlock = driver.findElement(By.xpath("(//div[@class='hotel-details-rooms__room-desc_wrap'])["+i+"]"));			  		  
		  hotelBlock.click();
		  WebElement hotelBlockParent = driver.findElement(By.xpath("(//div[@class='hotel-details-rooms__room ng-scope hotel-details-rooms__room_state_open'])["+i+"]"));
		  button = hotelBlockParent.findElement(By.cssSelector(".btn.btn-large.btn-green.btn-hotel-room-actions.ng-scope"));
		  if(hotelBlockParent.findElement(By.cssSelector("div.ng-scope > p.ng-binding")).getText().contains(
				  "приведут к уплате неустойки в сумме стоимости номера за сутки и налога, умноженной на 1. Данный отель не выплачивает компенсацию в случае неявки или преждевременного отъезда"))
		  {
			  break;  
		  }		  
	  }  
  	
  
	  if (button!=null)
	  {
		  button.click();
		  int h=0;
		  h++;
	  }

  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
